package demo1;
class Money implements Cloneable{
    double money=9.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public String name;
    public int age;
    Money m=new Money();
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp=(Person) super.clone();//实现person的克隆
        tmp.m=(Money)this.m.clone();//实现m的引用的克隆
        return tmp;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", money=" + m.money +
                '}';
    }
}
public class Test {
    public static void main(String[] args)throws CloneNotSupportedException {
        Person person=new Person("张三",18);
        Person person1=(Person) person.clone();
        System.out.println(person);
        System.out.println(person1);
        person.m.money=99;
        person1.m.money=88;
        System.out.println(person);
        System.out.println(person1);
    }
}
